package com.art.museumbackend.service.impl;

import com.art.museumbackend.entity.SystemLog;
import com.art.museumbackend.mapper.SystemLogMapper;
import com.art.museumbackend.service.SystemLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog> implements SystemLogService {

    @Override
    public Page<SystemLog> getLogList(String type, String username, LocalDateTime startTime, LocalDateTime endTime, int page, int size) {
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, SystemLog::getLogType, type)
                .like(username != null, SystemLog::getUsername, username)
                .ge(startTime != null, SystemLog::getCreateTime, startTime)
                .le(endTime != null, SystemLog::getCreateTime, endTime)
                .orderByDesc(SystemLog::getCreateTime);

        return page(new Page<>(page, size), wrapper);
    }

    @Override
    public void cleanLogs(String type, int days) {
        LocalDateTime cleanTime = LocalDateTime.now().minusDays(days);
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null && !type.equals("all"), SystemLog::getLogType, type)
                .le(SystemLog::getCreateTime, cleanTime);
        remove(wrapper);
    }

    @Override
    public byte[] exportLogs(String type, String username, LocalDateTime startTime, LocalDateTime endTime) {
        // 查询日志数据
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, SystemLog::getLogType, type)
                .like(username != null, SystemLog::getUsername, username)
                .ge(startTime != null, SystemLog::getCreateTime, startTime)
                .le(endTime != null, SystemLog::getCreateTime, endTime)
                .orderByDesc(SystemLog::getCreateTime);
        List<SystemLog> logs = list(wrapper);

        // 创建Excel工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("系统日志");

            // 创建标题行
            Row headerRow = sheet.createRow(0);
            String[] headers = {"用户名", "操作内容", "请求方法", "请求参数", "IP地址", "状态", "错误信息", "日志类型", "创建时间"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }

            // 填充数据
            int rowNum = 1;
            for (SystemLog log : logs) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(log.getUsername());
                row.createCell(1).setCellValue(log.getOperation());
                row.createCell(2).setCellValue(log.getMethod());
                row.createCell(3).setCellValue(log.getParams());
                row.createCell(4).setCellValue(log.getIp());
                row.createCell(5).setCellValue(log.getStatus());
                row.createCell(6).setCellValue(log.getErrorMsg());
                row.createCell(7).setCellValue(log.getLogType());
                row.createCell(8).setCellValue(log.getCreateTime().toString());
            }

            // 导出为字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return outputStream.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("导出日志失败", e);
        }
    }
} 